package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.RewardTask;

import java.util.List;

/**
 * 用户悬赏任务列表适配器
 */
public class UserRewardTaskAdapter extends RecyclerView.Adapter<UserRewardTaskAdapter.ViewHolder> {

    private Context context;
    private List<RewardTask> rewardTasks;
    private OnTaskClickListener listener;

    public interface OnTaskClickListener {
        void onTaskClick(RewardTask task);
        void onTaskAccept(RewardTask task);
    }

    public UserRewardTaskAdapter(Context context, List<RewardTask> rewardTasks, OnTaskClickListener listener) {
        this.context = context;
        this.rewardTasks = rewardTasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_reward_task, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RewardTask task = rewardTasks.get(position);
        
        // 设置任务标题
        holder.tvTitle.setText(task.getTitle());
        
        // 设置任务描述
        holder.tvDescription.setText(task.getDescription());
        
        // 设置任务奖励
        holder.tvReward.setText(String.format("¥%.2f", task.getReward()));
        
        // 设置剩余数量
        holder.tvRemainingOrders.setText(String.format("剩余: %d/%d", 
                task.getRemainingOrders(), task.getTotalOrders()));
        
        // 设置接取按钮状态
        if (task.isTaken() || task.getRemainingOrders() <= 0) {
            holder.btnAccept.setEnabled(false);
            holder.btnAccept.setText("已接取");
        } else {
            holder.btnAccept.setEnabled(true);
            holder.btnAccept.setText("接取任务");
            
            // 设置接取按钮点击事件
            holder.btnAccept.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onTaskAccept(task);
                }
            });
        }
        
        // 设置整个条目的点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskClick(task);
            }
        });
    }

    @Override
    public int getItemCount() {
        return rewardTasks.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvDescription;
        TextView tvReward;
        TextView tvRemainingOrders;
        Button btnAccept;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_reward_title);
            tvDescription = itemView.findViewById(R.id.tv_reward_description);
            tvReward = itemView.findViewById(R.id.tv_reward_price);
            tvRemainingOrders = itemView.findViewById(R.id.tv_reward_time);
            btnAccept = itemView.findViewById(R.id.btn_reward_action);
        }
    }
} 